package com.ermace.robo.jumper;

import android.app.Activity;
import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

public class WinScreen extends Activity {

	private SharedPreferences highscores;
	private SharedPreferences parameters;
	private Button saveHighscore;
	private TextView inputName;
	private String[] highscoreList;
	private int theScore;
	private SharedPreferences highscoresNames;
	private ImageView titleImg; 
	private Drawable newHS;
	private boolean backPressed = false;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.winscreen);

		highscores = getSharedPreferences("highScores", MODE_PRIVATE);
		parameters = getSharedPreferences("parameters", MODE_PRIVATE);
		highscoresNames = getSharedPreferences("highscoresNames", MODE_PRIVATE);
		theScore = parameters.getInt("lastScore", 0);
		highscoreList = highscores.getAll().keySet().toArray(new String[0]);
		highscoreList = HighScores.sortHighscores(highscoreList, highscores);
		
		saveHighscore = (Button) findViewById(R.id.saveHighscore);
		saveHighscore.setOnTouchListener(new OnTouchListener() {			
		@Override
		public boolean onTouch(View v, MotionEvent event) {
//				if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
				saveHighscore.setBackgroundResource(R.drawable.choosemap_button_pressed);
				
				switch(event.getAction()) {
//					case MotionEvent.ACTION_HOVER_EXIT:
//						saveHighscore.setBackgroundResource(R.drawable.choosemap_button);
//						break;
						
					case MotionEvent.ACTION_OUTSIDE:
						saveHighscore.setBackgroundResource(R.drawable.choosemap_button);
						break;
						
					case MotionEvent.ACTION_UP:
						saveHighscore.setBackgroundResource(R.drawable.choosemap_button);
						if(checkTop10(theScore))
							saveHighscore();
						MainActivity.startingNewActivity = true;
						finish();
						return true;
				}
				
				return true;
			}
		});
		
		if(!checkTop10(theScore))
		{
			saveHighscore.setText(R.string.playAgain);
			EditText input = (EditText) findViewById(R.id.inputNameText);
			input.setVisibility(View.GONE);
		}
		TextView scoreText = (TextView) findViewById(R.id.scoreText);
		scoreText.setText("" + theScore);
		
		titleImg = (ImageView) findViewById(R.id.titleImg);
		newHS =  getResources().getDrawable(R.drawable.new_highscore_03);  
		checkNewHighscore(theScore);
	}

	public void saveHighscore()
	{
		inputName = (TextView) findViewById(R.id.inputNameText);
		String name = inputName.getText().toString();
		setScore(name);
		finish();
	}

	private boolean setScore(String name) {
		
		SharedPreferences.Editor preferencesEditor = highscores.edit();
		SharedPreferences.Editor preferencesEditor2 = highscoresNames.edit();
		if(highscoreList.length < 10){			
			preferencesEditor.putInt(highscoreList.length +  "", theScore); // store current search
			preferencesEditor2.putString(highscoreList.length +  "", name); // store current search
			preferencesEditor2.commit();
			preferencesEditor.commit();	
		}
		else if(theScore > highscores.getInt(highscoreList[9], 0)){
			preferencesEditor.putInt(highscoreList[9], theScore);
			preferencesEditor2.putString(highscoreList[9], name); // store current search
			preferencesEditor2.commit();
			preferencesEditor.commit();
		}
		return false;
	}
	
	private void checkNewHighscore(int score)
	{
		if(highscoreList.length != 0)
		{
			if(score > highscores.getInt(highscoreList[0], 0))
				titleImg.setImageDrawable(newHS);
		}
	}
	
	public boolean checkTop10(int score)
	{
		if(highscoreList.length < 10 || score > highscores.getInt(highscoreList[9], 0))
			return true;
		return false;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.winscreen, menu);
		return true;
	}
	
    
	@Override
	protected void onResume() {
		super.onResume();
			MainActivity.resumeMusic();
	}
	
	@Override
	protected void onPause() {
		super.onPause();
			if(!backPressed )
				MainActivity.pauseMusic();
	}
	
	@Override
	public void onBackPressed() {
		backPressed = true;
		super.onBackPressed();
	}
}
